package com.yuyou.fn.educational.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.educational.entity.BaseGrade;
import com.yuyou.fn.educational.entity.Period;
import com.yuyou.fn.educational.entity.RefundSetting;
import com.yuyou.fn.educational.mapper.RefundSettingMapper;
import com.yuyou.fn.educational.service.IBaseGradeService;
import com.yuyou.fn.educational.service.IPeriodService;
import com.yuyou.fn.educational.service.IRefundSettingService;
import com.yuyou.fn.educational.vo.BusTeamVo;
import com.yuyou.fn.educational.vo.PeriodTeamVo;
import com.yuyou.fn.educational.vo.RefundSettingVo;
import com.yuyou.fn.educational.vo.RefundsettingBusteamVo;
import com.yuyou.fn.platform.entity.AreaTeam;
import com.yuyou.fn.platform.entity.BusTeam;
import com.yuyou.fn.platform.service.IAreaTeamService;
import com.yuyou.fn.platform.service.IBusTeamService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * RefundSetting 表数据服务层接口实现类
 */
@Service
public class RefundSettingServiceImpl extends SuperServiceImpl<RefundSettingMapper, RefundSetting, Long> implements IRefundSettingService {

    @Resource
    private IBusTeamService busTeamService;

    @Resource
    private IAreaTeamService areaTeamService;

    @Resource
    private IPeriodService periodService;

    @Resource
    private IBaseGradeService baseGradeService;

    @Override
    @Transactional
    public void saveOrUpdateRefundSetting(RefundSettingVo refundSettingVo) {
        List<RefundSetting> refundSettings = refundSettingVo.getRefundSettings();
        this.autoMapper.deleteRefundSettings(refundSettings);
        insertBatch(refundSettings);
    }

    @Override
    public List<RefundSetting> queryRefundSettings(Long areaTeamId, Long periodId) {
        Example<RefundSetting> example = RefundSetting.createExample();
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("areaTeamId", areaTeamId).andEqualTo("periodId", periodId);

        List<RefundSetting> refundSettings = findListByExample(example);
        return refundSettings;
    }

    @Override
    public RefundSetting queryOneRefundSetting(Long areaTeamId, Long periodId, Long busTeamId) {
        Example<RefundSetting> example = RefundSetting.createExample();
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("areaTeamId", areaTeamId).
                andEqualTo("periodId", periodId).andEqualTo("busTeamId", busTeamId);
        return findOneByExample(example);
    }

    @Override
    @Cacheable("CacheOfRefundSettingBusteams")
    public List<RefundsettingBusteamVo> queryRefundSettingBusteams() {
        List<AreaTeam> areaTeams = areaTeamService.findAllAreaTeam();
        List<BaseGrade> baseGrades = baseGradeService.findAllFromCache();
        List<RefundsettingBusteamVo> refundsettingBusteamVos = new ArrayList<>();
        for (AreaTeam areaTeam : areaTeams) {
            Long areaTeamId = areaTeam.getAreaTeamId();
            RefundsettingBusteamVo refundsettingBusteamVo = new RefundsettingBusteamVo();
            refundsettingBusteamVo.setAreaTeamId(areaTeamId);
            refundsettingBusteamVo.setAreamTeamName(areaTeam.getName());
            List<PeriodTeamVo> periodTeamVos = new ArrayList<>();
            List<Period> periods = periodService.findListByAreaTeamId(areaTeamId);
            for (Period period : periods) {
                PeriodTeamVo periodTeamVo = new PeriodTeamVo();
                periodTeamVo.setPeriodId(period.getPeriodId());
                periodTeamVo.setPeriodName(period.getPeriodName());
                List<BusTeamVo> busTeamVos = new ArrayList<>();
                List<BusTeam> busTeams = busTeamService.findBusTeamByAreaTeamIds(Arrays.asList(areaTeamId));
                for (BusTeam busTeam : busTeams) {
                    BusTeamVo busTeamVo = new BusTeamVo();
                    busTeamVo.setBusTaeamId(busTeam.getBusTeamId());
                    busTeamVo.setBudTeamName(busTeam.getName());
                    busTeamVo.setBaseGradeList(baseGrades);
                    busTeamVos.add(busTeamVo);
                }
                periodTeamVo.setBusTeamVoList(busTeamVos);
                periodTeamVos.add(periodTeamVo);
            }
            refundsettingBusteamVo.setPeriodTeamVoList(periodTeamVos);
            refundsettingBusteamVos.add(refundsettingBusteamVo);
        }
        return refundsettingBusteamVos;
    }

    @Override
    public void deleteRefundSetting(Long refundSettingId) {
        deleteById(refundSettingId);
    }
}